<?php
/*
 * webos.php
 * name:xiaojia
 * email:iatt@qq.com
 * qq:273142650
 * time:2012.1.14 15:42
*/

	require_once('user.php');

	$userClass = new userClass;

	class webosClass
	{
		
		public function get_application ($id = '', $type = 'list', $search = '') {
			
			global $wpdb;
			
			global $USER;
			
			global $userClass;
			
			$auth = $userClass -> getGroupAuth($USER['webos_group']);
			
			$where = "WHERE webosAppListID in ($auth) ";
			
			if ($id == '' && $type == '') {
				
				$search = preg_replace('/[\/\'\"\&\%\@\!]/i', '', $search);
				
				$where .= "AND appName like '$search%'";
				
			} else {
			
				if ($type == 'new') {
					
					$limit = 'LIMIT 20';
					
				} else {
					
					$menuData = Jdb::select("SELECT * FROM " . PRE . "plugin_menu WHERE webosPluginMenuID = $id LIMIT 1");
									
					$menuParentID = $menuData['pmParentID'];
					
					$menuLevel = (int)$menuData['pmLevel'];
					
					if ($menuParentID != 0) {
						
						$where .= "AND webosPluginMenuID = $id";
						
					} else {
					
						$where .= 'AND pmLevel >= ' . $menuLevel . ' AND pmLevel < ' . ($menuLevel + 1000);
					
					}
					
				}
				
			}
			
			return Jdb::getRs("SELECT * FROM `" . PRE . "application` $where ORDER BY appAddDate DESC $limit");
			
		}
		
		public function get_user_app ($userID) {
			
			global $USER;
			
			return Jdb::getRs("SELECT * FROM `vi_" . PRE . "user_application` WHERE userID = $userID ORDER BY uaSort ASC");
			
		}
		
		public function get_default_app () {
						
			return Jdb::getRs('SELECT *, appDefaultShow as uaScreen FROM `' . PRE . 'application` WHERE appDefaultShow != -1 AND appSystem = 0 ORDER BY appAddDate DESC');
			
		}
		
		public function get_plugin_menu ($type) {
			
			if ($type == 'set') {
				
				$where = "WHERE pmType = 'list'";
				
			}
			
			return Jdb::getRs('SELECT * FROM `' . PRE . 'plugin_menu` ' . $where . ' ORDER BY pmLevel ASC, pmSort ASC');
			
		}
		
		public function get_theme_list () {
		
			return Jdb::getRs('SELECT * FROM `' . PRE . 'theme` ORDER BY themeSort DESC, themeAddDate DESC');
		
		}
		
		public function get_application_config ($appID) {
			
			return Jdb::getRs("SELECT * FROM `" . PRE . "application_config` WHERE appID = '$appID'");
			
		}
		
		public function set_modify_theme ($href, $width, $height, $userID) {
			
			$data['userTheme'] = $href;
		
			$data['userThemeWidth'] = $width;
		
			$data['userThemeHeight'] = $height;
			
			Jdb::update(PRE . 'webosuser', $data, "WHERE userID = '$userID'");
			
		}
		
		public function set_moveuserapp ($screen, $appID, $userID) {
		
			$data['uaScreen'] = $screen;
			
			Jdb::update(PRE . 'user_application', $data, "WHERE webosAppListID = '$appID' AND userID = '$userID'");
		
		}
		
		public function set_removeuserapp ($appID, $userID) {
				
			Jdb::delete(PRE . 'user_application', "WHERE webosAppListID = '$appID' AND userID = $userID");
			
		}
		
		public function set_userapp ($appID, $screen, $userID) {
			
			$addDate = date('Y/m/d H:i:s');
			
			$data['userID'] = $userID;
			
			$data['webosAppListID'] = $appID;
			
			$data['uaScreen'] = $screen;
			
			$data['uaAddDate'] = $addDate;
			
			return Jdb::insert(PRE . 'user_application', $data);
			
		}
		
		public function set_userinfo ($userID) {
			
			$table = PRE . 'webosuser';
			
			$form_data = Jdb::data(array(
			
				'table' => $table,
				
				'type' => 'POST'
			
			));
			
			Jdb::update($table, $form_data, "WHERE userID = $userID");
			
		}
		
	}
?>